<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-10-29 14:56:33
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 工序收货，染色收货，倒毛收货
 */

namespace App\Models;

use App\Repositories\FinanceVoucherRepository;
use App\Traits\AttachmentTrait;
use App\Traits\AutoCodeTrait;
use App\Traits\MaterialTrait;
use App\Traits\StorageTrait;
use App\Traits\CarryTrait;
use App\Traits\CarTrait;
use App\Traits\VoucherTrait;

class ProProcessesStorage extends BaseModel
    {
    use MaterialTrait;
    use AutoCodeTrait;
    use StorageTrait;
    use CarryTrait;
    use CarTrait;
    use VoucherTrait;
    use AttachmentTrait;
    public $timestamps = true;
    public $table = "sys_pro_processes_storage";
    protected $guarded = [];
    protected $casts = [];
    // 
    protected $ACSign = 'DYEING_INSTORAGE';

    public function getSourceAddr()
        {
        return $this->outfactory->name;
        }
    //运输派车目的地址
    public function getTargetAddr()
        {
        return $this->location->name;
        }

    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_with_finance');
            });
        }
    public function direction()
        {
        return $this->belongsTo(Dic::class, "direction_id", "key")->where(function ($q)
            {
            $q->where("code", 'OutProductBack');
            });
        }
    public function outfactory()
        {
        return $this->belongsTo(Company::class);
        }
    public function process()
        {
        return $this->belongsTo(ProProcesses::class);
        }
    public function wip()
        {
        return $this->belongsTo(ProProcessesWip::class);
        }

    public function finance($order = null)
        {
        $v = $order ?? $this;

        if ($v->status_id == 2)
            return $v->voucher;

        if ($v->status_id != 1) {
            fail("单据非审核状态！，无法财审！");
            }
        fauditSave($v);
        $details = [];
        if ($v->direction_id == 1) {
            $gdata               = $v->toArray();
            $gdata['company_id'] = $gdata['outfactory_id'];
            $gdata['quantity']   = $gdata['material_quantity'];
            //借 委托加工物资-加工费 140802
            $debit_subject = 140802;
            //贷 应付账款 2202
            $credit_subject = 2202;
            $summary        = "委外加工费";
            $gdata['price'] = $v->process?->out_product_price ?? 0;
            if ($gdata['price'] == 0) {
                fail("工序【{$v->process->name}】需要设置默认委外加工单价！");
                }
            $amount = $gdata['price'] * $gdata['quantity'];

            //借方分录 
            $details[] = FinanceVoucherRepository::mergeVoucher($debit_subject, $v->finance_remark, $amount, 0, $gdata);
            //贷方分录
            $details[] = FinanceVoucherRepository::mergeVoucher($credit_subject, $summary, 0, $amount, $gdata);

            $voucher = FinanceVoucherRepository::CreateVoucher($v->billdate ?? $v->created_at, 1, $details, $v, $v->voucher?->id);
            return FinanceVoucherRepository::auditVoucher($voucher);
            }
        //审核凭证 
        }
    public function nofinance($orders = null)
        {
        $v = $orders ?? $this;
        if ($v->status_id == 1)
            return $v->voucher;
        if ($v->status_id != 2) {
            fail("非财审状态！，无法取消财审！");
            }
        if (($v->voucher?->status_id ?? 0) != 0) {
            fail("凭证【{$v->voucher->code}】已记账！ 操作失败！");
            }
        auditSave($v, false, true, 0);

        return $v;
        }
    }
